﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.IO;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Printing;
using Microsoft.Win32;

namespace Notepad_Clone
{
	partial class NotepadClone : Window
	{
		protected string strFilter = "Text Documents(*.txt)|*.txt|All Files(*.*)|*.*";

		void AddFileMenu(Menu menu)
		{
			MenuItem itemFile = new MenuItem();
			itemFile.Header = "_File";
			menu.Items.Add(itemFile);

			MenuItem itemNew = new MenuItem();
			itemNew.Header = "_New";
			itemNew.Command = ApplicationCommands.New;
			itemFile.Items.Add(itemNew);
			this.CommandBindings.Add(new CommandBinding(ApplicationCommands.New, this.NewOnExecute));

			MenuItem itemOpen = new MenuItem();
			itemOpen.Header = "_Open...";
			itemOpen.Command = ApplicationCommands.Open;
			itemFile.Items.Add(itemOpen);
			this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, this.OpenOnExecute));

			MenuItem itemSave = new MenuItem();
			itemSave.Header = "_Save";
			itemSave.Command = ApplicationCommands.Save;
			itemFile.Items.Add(itemSave);
			this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Save, this.SaveOnExecute));

			MenuItem itemSaveAs = new MenuItem();
			itemSaveAs.Header = "Save _As...";
			itemSaveAs.Command = ApplicationCommands.SaveAs;
			itemFile.Items.Add(itemSaveAs);
			this.CommandBindings.Add(new CommandBinding(ApplicationCommands.SaveAs, this.SaveAsOnExecute));

			itemFile.Items.Add(new Separator());
			this.AddPrintMenuItems(itemFile);
			itemFile.Items.Add(new Separator());

			MenuItem itemExit = new MenuItem();
			itemExit.Header = "E_xit";
			itemExit.Click += this.ExitOnClick;
			itemFile.Items.Add(itemExit);
		}

		private void ExitOnClick(object sender, RoutedEventArgs e)
		{
			this.Close();
		}

		#region command execute method
		protected virtual void NewOnExecute(object sender, ExecutedRoutedEventArgs args)
		{
			if (!this.OkToTrash())
			{
				return;
			}

			this.txtbox.Text = string.Empty;
			this.strLoadedFile = null;
			this.isFileDirty = false;
			this.UpdateTitle();
		}

		void OpenOnExecute(object sender, ExecutedRoutedEventArgs args)
		{
			if (!this.OkToTrash())
			{
				return;
			}

			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = this.strFilter;
			if (dlg.ShowDialog(this).GetValueOrDefault())
			{
				this.LoadFile(dlg.FileName);
			}
		}

		void SaveOnExecute(object sender, ExecutedRoutedEventArgs args)
		{
			if (this.strLoadedFile == null || this.strLoadedFile.Length == 0)
			{
				this.DisplaySaveDialog(string.Empty);
			}
			else
			{
				this.SaveFile(this.strLoadedFile);
			}
		}

		void SaveAsOnExecute(object sender, ExecutedRoutedEventArgs args)
		{
			this.DisplaySaveDialog(this.strLoadedFile);
		}
		#endregion

		#region private method
		/// <summary>
		/// 如果内容不需要保存，返回true
		/// </summary>
		/// <returns></returns>
		private bool OkToTrash()
		{
			if (!this.isFileDirty)
			{
				return true;
			}

			MessageBoxResult result = MessageBox.Show(
				"The text in the file " + this.strLoadedFile + " has changed\n\n" + "Do you want to save the changes?",
				this.strAppTitle,
				MessageBoxButton.YesNoCancel,
				MessageBoxImage.Question,
				MessageBoxResult.Yes);
			if (result == MessageBoxResult.Cancel)
			{
				return false;
			}
			else if (result == MessageBoxResult.No)
			{
				return true;
			}
			else
			{
				if (this.strLoadedFile != null && this.strLoadedFile.Length > 0)
				{
					return this.SaveFile(this.strLoadedFile);
				}

				return this.DisplaySaveDialog(string.Empty);
			}
		}

		private void LoadFile(string strFileName)
		{
			try
			{
				this.txtbox.Text = File.ReadAllText(strFileName);
			}
			catch (Exception ex)
			{
				MessageBox.Show(
					"Error on File Open: " + ex.Message,
					this.strAppTitle,
					MessageBoxButton.OK,
					MessageBoxImage.Asterisk);
			}

			this.strLoadedFile = strFileName;
			this.UpdateTitle();
			this.txtbox.SelectionStart = 0;
			this.txtbox.SelectionLength = 0;
			this.isFileDirty = false;
		}

		private bool SaveFile(string strFileName)
		{
			try
			{
				File.WriteAllText(strFileName, this.txtbox.Text);
			}
			catch (Exception ex)
			{
				MessageBox.Show(
					"Error on File Save: " + ex.Message,
					this.strAppTitle,
					MessageBoxButton.OK,
					MessageBoxImage.Asterisk);
				return false;
			}

			this.strLoadedFile = strFileName;
			this.UpdateTitle();
			this.isFileDirty = false;
			return true;
		}

		private bool DisplaySaveDialog(string strFileName)
		{
			SaveFileDialog dlg = new SaveFileDialog();
			dlg.Filter = this.strFilter;
			dlg.FileName = strFileName;
			if (dlg.ShowDialog(this).GetValueOrDefault())
			{
				this.SaveFile(dlg.FileName);
				return true;
			}

			return false;
		}
		#endregion
	}
}
